<template>
  <div class="addCoupon">
    <el-form :label-position="labelPosition" label-width="100px" :model="formData" :rules="rules" ref="ruleForm">
      <el-form-item label="优惠券名称:" prop="ticketName">
        <el-input v-model="formData.ticketName" style="width: 200px;"></el-input>
      </el-form-item>

      <el-form-item label="使用门槛:">
        <el-radio v-model="radioUse" label="0" @change="changeUse">无使用门槛</el-radio>
        <el-radio v-model="radioUse" label="1" @change="changeUse">
          订单满
          <el-input v-model="formData.useLimitMoney" size="mini" style="width: 70px;" placeholder="大于0" :disabled="useMoney"></el-input> 元
        </el-radio>
      </el-form-item>

      <el-form-item label="优惠内容:" prop="preferentialMoney">
        <el-radio v-model="radioType" label="1" @change="changeType">
          代金券 减免
          <el-input v-model="formData.preferentialMoney" size="mini" style="width: 70px;" placeholder="大于0" :disabled="preferentialTypes"></el-input> 元
        </el-radio>
        <el-radio v-model="radioType" label="2" @change="changeType">
          折扣券 享受
          <el-input v-model="formData.preferentialMoney" size="mini" style="width: 70px;" placeholder="大于0" :disabled="!preferentialTypes" :max="10"></el-input> 折优惠
        </el-radio>
      </el-form-item>

      <el-form-item label="使用规则:" prop="useType">
        <el-select v-model="formData.useType" placeholder="使用规则">
          <el-option
            v-for="item in ruleOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="券有效期:" prop="expiryDays">
        领券
        <el-input v-model="formData.expiryDays" size="mini" style="width: 70px;" placeholder="大于0"></el-input> 天内可用
      </el-form-item>
    </el-form>
    <div style="text-align: center;">
      <el-button @click="cancel">取消</el-button>
      <el-button type="warning" @click="submit('ruleForm')">创建优惠券</el-button>
    </div>

  </div>
</template>

<script>
  export default {
    name: 'addCoupon',
    data() {
      return {
        radioUse: '0',
        useMoney: true,
        preferentialTypes: false,
        radioType: '1',
        labelPosition: 'right',
        formData: {
          useLimitMoney: '0',
          preferentialType: '1',
        },
        ruleOptions: [
          {
            value: '1',
            label: '通用'
          }, 
          // {
          //   value: '2',
          //   label: '购买时长使用'
          // }, 
          {
            value: '3',
            label: '购买月租使用'
          }, {
            value: '4',
            label: '会员充值使用'
          }
        ],
        rules: {
          ticketName: [
            { required: true, message: '请输入活动名称', trigger: 'blur' },
            { min: 1, max: 12, message: '长度在 1 到 12 个字符', trigger: 'blur' }
          ],
          preferentialMoney: [
            { required: true, message: '请选择优惠内容', trigger: 'blur' },
          ],
          useType: [
            { required: true, message: '请选择规则类型', trigger: 'blur' },
          ],
          expiryDays: [
            { required: true, message: '请输入有效天数', trigger: 'blur' },
          ]
        }
      }
    },
    mounted() {

    },
    methods: {
      changeUse(e) {
        if(e == 0) {
          this.useMoney = true;
          this.formData.useLimitMoney = '0';
        } else {
          this.formData.useLimitMoney = null;
          this.useMoney = false
        }
      },
      changeType(e) {
        this.formData.preferentialType = e;
        this.preferentialTypes = !this.preferentialTypes;
      },
      submit(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$axios({
              method: 'post',
              url: '/promotion/ticket/addTicket',
              data: this.formData
            }).then((res) => {
              console.log(res)
              if(res.code == 200) {
                this.$message.success('创建优惠券成功！');
                this.$emit('closeDialog',false);
                this.$emit('refresh')
              }
            })
          } else {
            return false;
          }
        });
      },
      cancel() {
        this.$emit('closeDialog',false);
      },
      

    }
  }
</script>

<style scoped>



</style>
